﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using IDM.BF.AdministrationService;
using Sio.Mdm.Client;
using Ext.Net;
using Microsoft.Win32;

namespace IDM.Web
{
    public partial class Help : System.Web.UI.Page // Sio.Mdm.Web.Controls.Page
    {
        private bool HasFirebird
        {
            get
            {
                return true;
                /*
                return Registry.GetValue(
                    "HKEY_LOCAL_MACHINE\\Software\\Firebird Project\\Firebird Server\\Instances\\", 
                    "DefaultInstance", 
                    null
                ) != null;
                 */
            }
        }
        private bool HasDatabase
        {
            get
            {
                IAuthorizationService service = ServiceFactory.GetService<IAuthorizationService>();
                return service.HasDatabase() == null;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            UpdateStatus(null, new DirectEventArgs(null));
            base.OnLoad(e);
        }
        
        protected void CreateDatabse(object sender, DirectEventArgs e)
        {
            IAuthorizationService service = ServiceFactory.GetService<IAuthorizationService>();
            Sio.Mdm.Types.Result error = service.CreateDatabase();
            
            if (error != service.CreateDatabase())
            {
                e.Success = false;
                e.ErrorMessage = error.ToString();
            }

            UpdateStatus(sender, e);
        }
        protected void Login(object sender, DirectEventArgs e)
        {
            IAuthorizationService auth = ServiceFactory.GetService<IAuthorizationService>();
            Sio.Mdm.Types.Session session;
            Sio.Mdm.Types.Result error;

            error = auth.Login("admin", "admin12", out session);

            if (error == null)
            {
                FormsAuthentication.SetAuthCookie("admin", false);
                Response.Redirect("Default.aspx");
            }
            else
            {
                e.Success = false;
                e.ErrorMessage = error.ToString();
            }
        }
        protected void UpdateStatus(object sender, DirectEventArgs args)
        {
//            RegisterIcon(Icon.Accept);

            if (HasFirebird)
            {
                InstallFirebird.Icon = Icon.Accept;
                CreateDB.Disabled = false;
            }
            else
            {
                InstallFirebird.Icon = Icon.Cancel;
                CreateDB.Disabled = true;
            }

            if (HasDatabase)
            {
                CreateDB.Icon = Icon.Accept;
            }
            else
            {
                CreateDB.Icon = Icon.Cancel;
                LoginBtn.Disabled = true;
            }

            if (HasDatabase && HasFirebird)
            {
                LoginBtn.Icon = Icon.Accept;
                LoginBtn.Disabled = false;
            }
        }
    }
}
